package com.itwxz.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itwxz.common.utils.bean.BeanUtils;
import com.itwxz.system.domain.po.WxzOrder;
import com.itwxz.system.domain.po.WxzOrderHistory;
import com.itwxz.system.domain.dto.SelectHistoryOrderDto;
import com.itwxz.system.domain.vo.SearchOrderVo;
import com.itwxz.system.mapper.WxzOrderHistoryMapper;
import com.itwxz.system.service.IWxzOrderHistoryService;
import com.itwxz.system.service.IWxzOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 历史订单Service业务层处理
 *
 * @author gastronome
 * @date 2025-04-06
 */
@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
public class WxzOrderHistoryServiceImpl extends ServiceImpl<WxzOrderHistoryMapper, WxzOrderHistory> implements IWxzOrderHistoryService {

	private final WxzOrderHistoryMapper orderHistoryMapper;

	/**
	 * 查询历史订单列表
	 *
	 * @param orderDto 历史订单
	 * @return 历史订单
	 */
	@Override
	public List<SelectHistoryOrderDto> selectWxzOrderHistoryList(SelectHistoryOrderDto orderDto) {
		return orderHistoryMapper.selectWxzOrderHistoryList(orderDto);
	}

	@Override
	public List<Map<String, Object>> orderStatistics() {
		// 创建 QueryWrapper
		QueryWrapper<WxzOrderHistory> queryWrapper = new QueryWrapper<>();

		// 按 type 字段分组，统计数量
		queryWrapper.select("type", "COUNT(*) AS order_count")
				.groupBy("type")
				.orderByAsc("type");

		// 执行查询，返回结果
		return orderHistoryMapper.selectMaps(queryWrapper);
	}

	@Override
	public List<SearchOrderVo> searchOrderWithStaff(String keyword) {
		return orderHistoryMapper.searchOrderWithStaff(keyword);
	}

	@Override
	public Integer archiveAllOrders() {
		return orderHistoryMapper.archiveAllOrders();
	}
}
